<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2008 Johannes Held <uni@hehejo.de>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

require_once(PATH_tslib.'class.tslib_pibase.php');
require_once(t3lib_extMgm::extPath('univis2typo3') . 'common/class.tx_univis2typo3_person.php');


/**
 * Plugin 'contact box' for the 'univis2typo3' extension.
 *
 * @author	Starbugs <univis2typo3@googlegroups.com>
 * @package	TYPO3
 * @subpackage	tx_univis2typo3
 */
class tx_univis2typo3_pi7 extends tslib_pibase {
	var $prefixId      = 'tx_univis2typo3_pi7';		// Same as class name
	var $scriptRelPath = 'pi7/class.tx_univis2typo3_pi7.php';	// Path to this script relative to the extension dir.
	var $extKey        = 'univis2typo3';	// The extension key.
	
	/**
	 * The main method of the PlugIn
	 *
	 * @param	string		$content: The PlugIn content
	 * @param	array		$conf: The PlugIn configuration
	 * @return	The content that is displayed on the website
	 */
	function main($content='',$conf='') {
		//$this->conf = $conf;
		$this->pi_setPiVarDefaults();
		$this->pi_loadLL();
		$this->pi_USER_INT_obj=1;	// Configuring so caching is not expected. This value means that no cHash params are ever set. We do this, because it's a USER_INT object!
		
		$page_select = t3lib_div::makeInstance("t3lib_pageSelect");
		$page_select->init(false);

		$templateTotal = $this->cObj->fileResource($conf['templateFile']);
		$template = $this->cObj->getSubpart($templateTotal, "###TMPL_CONTACT_INFO###");
		
		$language = $GLOBALS["TSFE"]->sys_language_uid;
		/*if ($language == 0) {
		    $lehrstuhlname = $row['name_en'];
		    $row['ort'] = $row['ort'].'<br />Germany';
		} else {
		    $lehrstuhlname = $row['name'];		    
		}
		*/
		//$personHelper = new tx_univis2typo3_person($conf);
		$personHelper = t3lib_div::makeInstance("tx_univis2typo3_person", $conf);
		$cData = $personHelper->loadContactData();

		$markers = array();
		// fields which are always present
		$encryptMail = $this->cObj->getMailTo($cData['email'],$cData['email']);
		$markers['###T_CI_EMAIL###']  = $encryptMail[0];
		$markers['###T_CI_PHONE###']  = $cData['tel'];
		$markers['###T_CI_FAX###']    = $cData['fax'];
		$markers['###T_CI_STREET###'] = $cData['street'];
		$markers['###T_CI_CITY###']   = $cData['ort'] . (0==$language ? '<br />Germany' : '');
		// conditional fields
		if ($cData['office']) {
			$cData['office'] = '<tr>'
					. '<td><img alt="Raum" style="margin-right: 1mm;" align="middle" border="0" src="/images/contact_office.png" /></td>'
					. '<td>'.$cData['office'].'</td>'
					. '</tr>';
			$markers['###T_CI_ROOM###'] = $cData['office'];
		} else {
			$markers['###T_CI_ROOM###'] = '';
		}
		$markers['###T_CI_NAME###'] = (!$cData['name']
			       			? ($cData['persName'] ? $cData['persName'] : $cData['email'])
						: (0==$language ? $cData['name_en'] : $cData['name']) . '<br />');

		$this->pi_initPIflexForm();
		$heading = $this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'customheading', 'sDEF');
		if ($heading) {
			$markers['###T_CI_HEADING###'] = $heading;
		} else {
			$markers['###T_CI_HEADING###'] = (0==$language ? 'Contact' : 'Kontakt');
		}
		
		$preamble = $this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'preamble', 'sDEF');
		if ($preamble) {
			$markers['###T_CI_PREAMBLE###'] = '<p>' . $preamble . '</p>';
		} else {
			$markers['###T_CI_PREAMBLE###'] = '';
		}

		$addParagraph = $this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'insertparagraph', 'sDEF');
		if ($addParagraph) {
			$template = $template . '<p><br /></p>';
		}

		$content = $this->cObj->substituteMarkerArray($template, $markers);
		return $this->pi_wrapInBaseClass($content);
	}
}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/univis2typo3/pi7/class.tx_univis2typo3_pi7.php']) {
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/univis2typo3/pi7/class.tx_univis2typo3_pi7.php']);
}

?>
